#pragma once

#include <string>

namespace solution_006
{

class Solution
{
public:
    std::string convert(std::string s, int numRows)
    {
        if (numRows == 1)
            return s;
        int T = numRows * 2 - 2;

        std::string output;
        output.reserve(s.size());

        /* first row */
        for (int i = 0; i < (int)s.size(); i += T)
            output.push_back(s.at(i));

        for (int r = 1; r + 1 < numRows; ++r) {
            int k = (numRows - r - 1) * 2;
            for (int i = r; i < (int)s.size(); i += T) {
                output.push_back(s.at(i));
                if (i + k < (int)s.size())
                    output.push_back(s.at(i + k));
            }
        }

        /* last row */
        for (int i = numRows - 1; numRows > 1 && i < (int)s.size(); i += T)
            output.push_back(s.at(i));

        return output;
    }
};

}  /* namespace solution_006 */
